package com.funcourse.demo.api.restful.io.response.busi;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.funcourse.demo.model.CompeteRoom;
import com.funcourse.demo.model.CompeteRoom.RoomType;
import com.funcourse.demo.model.Quiz;
import com.funcourse.demo.model.UserWithScore;
import com.funcourse.demo.model.UserWithScore.Score;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CompeteRoomHttpVO extends CompeteRoomSocketVO{
    //original fields
    Long deadLine;
    Long startTime;
    String organizer;
    RoomType roomType;
    Long orgnizedTime;
    String qrCodeUrl;
    Quiz quiz;
    Long systemTime;
    public CompeteRoomHttpVO(CompeteRoom source,String toUserId,Integer topUserAmount,Boolean myScoreDetails){
        super();
        systemTime = System.currentTimeMillis();
        BeanUtils.copyProperties(source, this);
        final List<UserWithScore> sourceUserWithScoreList = source.getUserWithScoreList();
        this.userCount = sourceUserWithScoreList.size();

        //build topNUsers
        final List<UserWithScore> orginalUserWithScoreList = sourceUserWithScoreList
            .subList(0, userCount > topUserAmount ? topUserAmount : userCount);
        //减少带宽
        orginalUserWithScoreList.forEach(userWithScore -> {
            UserWithScore us = new UserWithScore();
            BeanUtils.copyProperties(userWithScore, us);
            us.setScoreDetails(null);
            this.topNUsers.add(us);
        });

        //build myScore
        for (UserWithScore userWithScore : sourceUserWithScoreList) {
            if (userWithScore.getId().equals(toUserId)) {
                myScore = new UserWithScore();
                BeanUtils.copyProperties(userWithScore, myScore);
                //减少带宽
                if (!myScoreDetails) {
                    myScore.setScoreDetails(null);
                }
                break;
            }
        }
        Integer myOnGoingQuestionNumber = 1000;
        if(null != myScore){
            myOnGoingQuestionNumber = myScore.getMyOnGoingQuestionNumber();
        }
        final Integer myOnGoingQuestionNumberFinal = myOnGoingQuestionNumber;
        //get top n users of question number
        topNUsers = sourceUserWithScoreList.stream()
            .map(userWithScore -> {
                UserWithScore userWithScoreTemp = new UserWithScore();
                BeanUtils.copyProperties(userWithScore, userWithScoreTemp);
                final List<Score> filteredScoreDetails = userWithScore.getScoreDetails().stream()
                    .filter(score -> score.getQuestionNumber() <= myOnGoingQuestionNumberFinal).map(score -> {
                        Score scoreTemp = new Score();
                        BeanUtils.copyProperties(score, scoreTemp);
                        return scoreTemp;
                    }).collect(Collectors.toList());
                userWithScoreTemp.setScoreDetails(filteredScoreDetails);
                Integer totalScore = 0;
                for (Score s : filteredScoreDetails) {
                    totalScore += s.getScore();
                }
                userWithScoreTemp.setTotalScore(totalScore);
                return userWithScoreTemp;
            }).sorted(Comparator.comparing(UserWithScore::getTotalScore).reversed()).limit(topUserAmount)
            .collect(Collectors.toList());
    }
}
